package service

import (
	"databureau/secret"
	"encoding/json"
	"net/http"
	"sync"

	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
)

const baseUrl_stock = "http://2.37.106.181:9003/esb/XZSPJ/Xzspj/services/"

// 获取乡镇的当日取号信息
func GetCountLegalPersonData(c *gin.Context) {
	url := baseUrl_stock + "XzspjCountLegalPersonData"
	year := c.Query("year")
	requestParams := map[string]string{
		"year": year,
	}
	Response, err := Get[ResponseBodyStock[[]CountLegalPersonData]](url, requestParams, c)
	if err != nil {
		c.JSON(http.StatusOK, ErrorResponse(0, err))
		return
	}
	//解密
	for i := 0; i < len(Response.Result); i++ {
		Response.Result[i].CodeType = secret.DecryptWords(Response.Result[i].CodeType)
		Response.Result[i].TotalRegCapital = secret.DecryptWords(Response.Result[i].TotalRegCapital)
		Response.Result[i].TotalLegalPerson = secret.DecryptWords(Response.Result[i].TotalLegalPerson)
	}

	c.JSON(http.StatusOK, SuccessResponse(Response.Result))
}

// 中介按项目获取热门事项信息
func GetWjzjHotTop5(c *gin.Context) {
	var params TokenParams[string]
	if err := c.ShouldBindJSON(&params); err != nil {
		url := baseUrl_stock + "XzspjWjzjHotTop5"
		data := []byte(`{"token": "` + params.Token + `"}`)
		nearlyResponse, err1 := Post[ResponsePack[DataListResponse[HotTop5Item]]](url, data, c)

		if err1 != nil {
			c.JSON(http.StatusOK, ErrorResponse(0, err1))
			return
		}
		var datas = nearlyResponse.Custom.DataList
		c.JSON(http.StatusOK, SuccessResponse(datas))
	} else {
		c.JSON(http.StatusOK, ErrorResponse(0, err))
	}
}

// 中介按项目获取热门事项详情信息
func GetWjzjHotTop5Detail(c *gin.Context) {
	var params TokenParams[HotTop5Detail]
	if err := c.ShouldBindJSON(&params); err == nil {
		url := baseUrl_stock + "XzspjWjzjHotTop5Detail"
		jsonData, errJson := json.Marshal((params))
		if errJson != nil {
			logrus.Error((errJson))
		}
		data := []byte(jsonData)
		nearlyResponse, err1 := Post[ResponsePack[DataListResponse[HotTop5DetailItem]]](url, data, c)
		if err1 != nil {
			c.JSON(http.StatusOK, ErrorResponse(0, err1))
			return
		}
		var datas = nearlyResponse.Custom
		c.JSON(http.StatusOK, SuccessResponse(datas))
	} else {
		c.JSON(http.StatusOK, ErrorResponse(0, err))
	}
}

// 中介按机构获取热门事项信息
func GetHotTaskTop5(c *gin.Context) {
	var params TokenParams[string]
	if err := c.ShouldBindJSON(&params); err != nil {
		url := baseUrl_stock + "XzspjWjzjGetHotTaskTop5"
		data := []byte(`{"token": "` + params.Token + `"}`)
		nearlyResponse, err1 := Post[ResponsePack[DataListResponse[HotTop5Item]]](url, data, c)
		if err1 != nil {
			c.JSON(http.StatusOK, ErrorResponse(0, err1))
			return
		}
		var datas = nearlyResponse.Custom.DataList
		c.JSON(http.StatusOK, SuccessResponse(datas))
	} else {
		c.JSON(http.StatusOK, ErrorResponse(0, err))
	}
}

// 中介按项目获取热门事项详情信息
func GetHotTaskTop5Detail(c *gin.Context) {
	var params TokenParams[HotTop5Detail]
	if err := c.ShouldBindJSON(&params); err == nil {
		url := baseUrl_stock + "XzspjWjzjGetHotTaskTop5Detail"
		jsonData, errJson := json.Marshal((params))
		if errJson != nil {
			logrus.Error((errJson))
		}
		data := []byte(jsonData)
		nearlyResponse, err1 := Post[ResponsePack[DataListResponse[HotTaskTop5DetailItem]]](url, data, c)
		if err1 != nil {
			c.JSON(http.StatusOK, ErrorResponse(0, err1))
			return
		}
		var datas = nearlyResponse.Custom
		c.JSON(http.StatusOK, SuccessResponse(datas))
	} else {
		c.JSON(http.StatusOK, ErrorResponse(0, err))
	}
}

// 获取乡镇的当日取号信息
func GetCountryQhInfo(c *gin.Context) {
	url := baseUrl_stock + "XzspjGetCountryQhInfo"
	Response, err := Get[CountryQhInfo](url, nil, c)
	if err != nil {
		c.JSON(http.StatusOK, ErrorResponse(0, err))
		return
	}
	c.JSON(http.StatusOK, SuccessResponse(Response))
}

// 获取近三年各区镇办件数量
func GetNearlyBjl(c *gin.Context) {
	url := baseUrl_stock + "XzspjGetNearlyBjl"
	data := []byte(`{}`)
	Response, err1 := Post[ResponsePack[DataListResponse[NearlyBjlList]]](url, data, c)

	if err1 != nil {
		c.JSON(http.StatusOK, ErrorResponse(0, err1))
		return
	}
	c.JSON(http.StatusOK, SuccessResponse(Response.Custom.DataList))
}

// 获取事项信息
func GetTaskNum(c *gin.Context) {
	url := baseUrl_stock + "XzspjGetTaskNum"
	data := []byte(`{}`)
	Response, err1 := Post[ResponsePack[TaskNum]](url, data, c)
	if err1 != nil {
		c.JSON(http.StatusOK, ErrorResponse(0, err1))
		return
	}
	c.JSON(http.StatusOK, SuccessResponse(Response.Custom))
}

// 获取事项信息
func GetTaskByShenpilb(c *gin.Context) {
	url := baseUrl_stock + "XzspjGetTaskByShenpilb"
	var params TaskByShenpilbRequest
	if err := c.ShouldBindJSON(&params); err == nil {
		requestParams := map[string]string{
			"shenpilb": params.Shenpilb,
		}
		jsonData, errJson := json.Marshal((requestParams))
		if errJson != nil {
			logrus.Error((errJson))
		}
		data := []byte(jsonData)
		Response, err1 := Post[ResponsePack[TaskByShenpilbResponse]](url, data, c)
		if err1 != nil {
			c.JSON(http.StatusOK, ErrorResponse(0, err1))
			return
		}
		c.JSON(http.StatusOK, SuccessResponse(Response.Custom))
	} else {
		c.JSON(http.StatusOK, ErrorResponse(0, err))
	}
}

// 获取乡镇的各个事项当天的取号数量
func GetCenterTaskTypeQhCount(c *gin.Context) {
	url := baseUrl_stock + "XzspjGetCenterTasktypeQhCount"
	Response, err := Get[CenterTaskTypeQhCount](url, nil, c)
	if err != nil {
		c.JSON(http.StatusOK, ErrorResponse(0, err))
		return
	}
	c.JSON(http.StatusOK, SuccessResponse(Response))
}

// 一体机异地联办市、区级地区明细获取接口
func GetCangoRegionList(c *gin.Context) {
	url := baseUrl_stock + "XzspjYtjYdlbGetCangoRegionList"
	var wg sync.WaitGroup
	ch := make(chan string, 1)
	wg.Add(1)
	go CreateToken(&wg, ch)
	wg.Wait()
	close(ch)
	token := <-ch
	var params CangoRegionRequest
	if err := c.ShouldBindJSON(&params); err == nil {
		var requestParams CangoRegionListRequest
		requestParams.Token = token
		requestParams.Params.Code = params.Code
		requestParams.Params.Level = params.Level
		requestParams.Params.Appkey = "8f2f075f-8d9c-4294-b92f-c3b8f6b99908"
		requestParams.Params.Appsecret = "8ee9fd1b-feed-4b3d-ac21-42ea914ab6a9"
		requestParams.Params.Sign = "54:B2:03:9A:92:11"

		jsonData, errJson := json.Marshal((requestParams))
		if errJson != nil {
			logrus.Error((errJson))
		}
		data := []byte(jsonData)
		Response, err1 := Post[ResponsePack[CangoRegionListResponse]](url, data, c)
		if err1 != nil {
			c.JSON(http.StatusOK, ErrorResponse(0, err1))
			return
		}
		c.JSON(http.StatusOK, SuccessResponse(Response.Custom.Regionlist))
	} else {
		c.JSON(http.StatusOK, ErrorResponse(0, err))
	}

}

func GetTaskAndCount(c *gin.Context) {
	var params TimeRange
	if err := c.ShouldBindJSON(&params); err == nil {
		url := "http://10.93.0.88:8086//wjzwfw/rest/wjbigscreen/getnewtaskandcount"
		data := []byte(`{"params":{}}`)
		nearlyResponse, err1 := Post[ResponsePack[TaskAndCountResponse]](url, data, c)
		if err1 != nil {
			c.JSON(http.StatusOK, ErrorResponse(0, err1))
			return
		}
		var datas = nearlyResponse.Custom.LevelArray
		c.JSON(http.StatusOK, SuccessResponse(datas))
	} else {
		c.JSON(http.StatusOK, ErrorResponse(0, err))
	}
}
